package org.hxyjs.service.impl;


import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.hxyjs.dao.ParentsDao;
import org.hxyjs.dao.RedisDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Account;
import org.hxyjs.entity.Parents;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.MailSendService;
import org.hxyjs.service.ParentsService;
import org.hxyjs.utils.GenNumTools;
import org.hxyjs.utils.MailMessageUtils;
import org.hxyjs.vo.ParentsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Service("parentsService")
public class ParentsServiceImpl implements ParentsService, AccountService {
    @Resource
    ParentsDao parentsDao;
    @Resource
    MailSendService mailSendService;
    @Resource
    RedisDao redisDao;
    @Override
    public DTO<Parents> getAllParentss()  throws Exception{
        List<Parents> parentss = parentsDao.getAllParentss();
        MessageEnum messageEnum  = Optional.ofNullable(parentss).isPresent()?MessageEnum.GETPARENTSSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parentss);
    }

    @Override
    public DTO<Parents> deleteParentsByPno(String pno)  throws Exception{
        Integer result = parentsDao.deleteParentsByPno(pno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEPARENTSSUCCESS:MessageEnum.DELETEPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Parents> updateParentsByPno(Parents parents)  throws Exception{
        Integer result = parentsDao.updateParentsByPno(parents);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEPARENTSSUCCESS:MessageEnum.UPDATEPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Parents> addParents(Parents parents) throws Exception {
        parents.setPstate("未激活");
        Integer result = parentsDao.addParents(parents);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDPARENTSSUCCESS:MessageEnum.ADDPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parents);
    }

    @Override
    public DTO<Parents> getParentsByPage(PageInfo pageInfo ) throws Exception {
        List<Parents> parentss = parentsDao.getParentsByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(parentss).isPresent()?MessageEnum.ADDADMINSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parentss,pageInfo);
    }

    @Override
    public DTO<Parents> selectParentsWithChildren(String pno)  throws Exception{
        Parents parents = parentsDao.selectParentsWithChildren(pno);
        MessageEnum messageEnum  = Optional.ofNullable(pno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parents);
    }

    @Override
    public DTO<Parents> getParentsWithOrdrees(String pno) throws Exception {
        Parents parents = parentsDao.getParentsWithOrdrees(pno);
        MessageEnum messageEnum  = Optional.ofNullable(pno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parents);
    }

    @Override
    public DTO<Parents> sendCodeMail(String pname) throws Exception {
        List<Parents> parentss = parentsDao.getParentsByExample(pname);
        DTO<Parents> dto = null;
        MessageEnum messageEnum  = null;
       if(!Optional.ofNullable(parentss).isPresent()){
           messageEnum = Optional.ofNullable(parentss).isPresent()?MessageEnum.GETPARENTSBYEXAMPLESUCCESS:MessageEnum.GETPARENTSBYEXAMPLEFAIL;
       }else{
           String code = GenNumTools.addZero(4,(Math.abs(new Random().nextInt()%1000)));
           redisDao.set(parentss.get(0).getPno(),code);
           String[] mail = MailMessageUtils.getMessage(parentss.get(0),"Parents:sendCode;"+code);
           mailSendService.sendMail(parentss.get(0),mail[0],mail[1]);
           messageEnum = MessageEnum.ADDACTIVITYPESUCCESS;
       }
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(), parentss);
    }

    @Override
    public DTO<Parents> updateParentsByPwd(ParentsVo parentsVo) throws Exception {
        String code = redisDao.get(parentsVo.getPno());
        if(!parentsVo.getCode().equals(code)){
            return new DTO<>(401,"验证码不正确");
        }
        Parents parents = new Parents();
        parents.setPno(parentsVo.getPno());
        parents.setPname(parentsVo.getUsername());
        String algorithmName = "md5";
//        String parentsname = parentsVo.getPrentsname();
        String parentsname = parentsVo.getUsername();
        String password = parentsVo.getPassword();
        String salt1 = parentsname;
        String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
        int hashIterations = 3;
        SimpleHash hash = new SimpleHash(algorithmName, password, salt1 + salt2, hashIterations);//参数1选择md5加密方式，参数2 原始密码  参数3 一个特定值（salt1 用户名 ）
        // +salt 2随机值 作为加工的佐料，参数4 佐料加的次数
        String encodedPassword = hash.toHex();//hash出来的是128位的2进制方便存储转换为32为的字符串
        System.out.println("000000000000000000000000");
        System.out.println(encodedPassword);
        parents.setPpwd(encodedPassword);
        parentsDao.updateParentsByPno(parents);
        MessageEnum messageEnum  = Optional.ofNullable(parentsVo).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Parents>(messageEnum.getCode(),messageEnum.getMessage(),parents);
    }

    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        Parents parents = parentsDao.getAccountByAccountName(accountName);
        System.out.println(parents+"--------666666---------");
        MessageEnum messageEnum  = Optional.ofNullable(accountName).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Account>(messageEnum.getCode(),messageEnum.getMessage(),parents);
    }



    @Override
    public DTO<Parents> activate(String pno) throws Exception {
        Parents parents = new Parents();
        parents.setPno(pno);
        parents.setPstate("已激活");
        Integer result = parentsDao.updateByPrimaryKeySelective(parents);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<Parents>(messageEnum.getCode(), messageEnum.getMessage());
    }



}
